package com.mysteel.order.web;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.mysteel.order.config.AlipayTemplate;
import com.mysteel.order.service.OrderService;
import com.mysteel.order.vo.PayVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * ProjectName: mail
 * ClassName: AliPayController
 * Description:
 * date: 2021/04/02/002 9:34
 *
 * @author xiaochao
 */
@Slf4j
@Controller
public class AliPayController {

    @Autowired
    private AlipayTemplate alipayTemplate;

    @Autowired
    private OrderService orderService;

    @ResponseBody
    @GetMapping(value = "/payOrder",produces = "text/html")
    public String payOrder(@RequestParam("orderSn") String orderSn){
        PayVo payVo = orderService.getOrderPay(orderSn);
        try {
            return alipayTemplate.pay(payVo);
        } catch (AlipayApiException e) {
            log.error("调用支付宝支付失败：{}", JSONObject.toJSONString(payVo));
        }
        return "支付宝支付失败";
    }
}
